﻿using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using System;
using ApplicationLibrary;

namespace Plumber.Accounting.ModuleData
{
    internal class RadarJournalEntryParam : RadarParameters
    {
        public RadarJournalEntryParam(string journalNo)
        {
            Params = new List<object>();
            Params.Add(journalNo);
        }
    }

    internal class RadarJETemplatesParam : RadarParameters
    {
        public RadarJETemplatesParam(string template)
        {
            Params = new List<object>();
            Params.Add(template);
        }
    }

    internal class RadarLiquidationTaxParam : RadarParameters
    {
        public RadarLiquidationTaxParam(int year, int month)
        {
            Params = new List<object>();
            Params.Add(year);
            Params.Add(month);
        }
    }

    internal class RadarLiquidationTypeParam : RadarParameters
    {
        public RadarLiquidationTypeParam(int year)
        {
            Params = new List<object>();
            Params.Add(year);
        }
    }

    #region Radar RadarJournalEntry
    internal class RadarJournalEntry : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1, p2, pFrom, pTo;
        private bool taxManagement = false;

        public RadarJournalEntry(bool taxmanagement)
            : base()
        {
            rdrCodeColumn = PL_JournalEntry.JournalNo.Name;
            rdrDescColumn = PL_JournalEntry.Description.Name;
            rdrNameSpace = "Plumber.Plumber.Accounting.Forms.JournalEntries";
            taxManagement = taxmanagement;
        }

        protected override Table GetRadarTable()
        {
            return new PL_JournalEntry();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            qb.Clear();
            qb.AddSelectAll(PL_JournalEntry.Name);
            qb.AddFrom(PL_JournalEntry.Name);
            qb.AddCompare(PL_JournalEntry.JournalNo, "@p1");

            p1 = new SqlABParameter("@p1", PL_JournalEntry.JournalNo);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param.Params[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PL_JournalEntry.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            p2 = new SqlABParameter("@p2", PL_JournalEntry.TaxRegister);

            qb.Clear();
            qb.AddSelectAll(PL_JournalEntry.Name);
            qb.AddFrom(PL_JournalEntry.Name);
            qb.AddCompare(PL_JournalEntry.TaxRegister, "@p2", taxManagement ? "<>" : "=");

            pFrom = new SqlABParameter("@p3", PL_JournalEntry.PostingDate);
            pTo = new SqlABParameter("@p4", PL_JournalEntry.PostingDate);

            qb.AddBetween(PL_JournalEntry.PostingDate, pFrom, pTo);

            pFrom.Value = GlobalInfo.CurrentDate.StartYear().AddDays(-180);
            pTo.Value = GlobalInfo.CurrentDate.EndYear();

            sqlCmd.Parameters.Add(pFrom);
            sqlCmd.Parameters.Add(pTo);

            qb.AddFilter(findQuery);

            qb.AddOrder(PL_JournalEntry.JournalNo);

            sqlCmd.Parameters.Add(p2);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p2.Value = (int)TaxRegisterType.E_NORegister;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarJournalEntryParam(DataGridViewRowValues.GetValue<string>(row, PL_JournalEntry.JournalNo));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarJournalEntryParam(text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }
    }
    #endregion Radar RadarJournalEntry

    #region Radar RadarJETemplates
    internal class RadarJETemplates : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;
        private SqlABParameter p2;

        public RadarJETemplates()
            : base()
        {
            rdrCodeColumn = PL_JETemplate.Template.Name;
            rdrDescColumn = PL_JETemplate.Description.Name;
            rdrNameSpace = "Plumber.Plumber.Accounting.Forms.JournalEntries";
        }

        protected override Table GetRadarTable()
        {
            return new PL_JETemplate();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            qb.Clear();
            qb.AddSelectAll(PL_JETemplate.Name);
            qb.AddFrom(PL_JETemplate.Name);
            qb.AddCompare(PL_JETemplate.Template, "@p1");

            p1 = new SqlABParameter("@p1", PL_JETemplate.Template);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param.Params[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PL_JETemplate.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            p2 = new SqlABParameter("@p2", PL_JETemplate.Template);

            qb.Clear();
            qb.AddSelectAll(PL_JETemplate.Name);
            qb.AddFrom(PL_JETemplate.Name);
            qb.AddFilter(findQuery);

            sqlCmd.Parameters.Add(p2);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p2.Value = PL_JETemplate.Template.DefaultValue;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarJETemplatesParam(DataGridViewRowValues.GetValue<string>(row, PL_JETemplate.Template));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarJETemplatesParam(text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }
    }

    #endregion Radar RadarJETemplates

    #region Radar RadarLiquidationTax
    internal class RadarLiquidationTax : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;
        private SqlABParameter p2;

        public RadarLiquidationTax()
            : base()
        {
            rdrCodeColumn = PL_LiquidationTax.Year.Name;
            rdrDescColumn = "";
            rdrNameSpace = "Plumber.Plumber.Accounting.Forms.LiquidationTax";
        }

        protected override Table GetRadarTable()
        {
            return new PL_LiquidationTax();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            qb.Clear();
            qb.AddSelectAll(PL_LiquidationTax.Name);
            qb.AddFrom(PL_LiquidationTax.Name);
            qb.AddCompare(PL_LiquidationTax.Year, "@p1");
            qb.AddCompare(PL_LiquidationTax.Month, "@p2");

            p1 = new SqlABParameter("@p1", PL_LiquidationTax.Year);
            p2 = new SqlABParameter("@p2", PL_LiquidationTax.Month);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);
            sqlCmd.Parameters.Add(p2);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param.Params[0];
            p2.Value = param.Params[1];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = ""; // sqlReader[PL_JETemplate.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            qb.Clear();
            qb.AddSelectAll(PL_LiquidationTax.Name);
            qb.AddFrom(PL_LiquidationTax.Name);
            qb.AddFilter(findQuery);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            //p2.Value = PL_LiquidationTax.Template.DefaultValue;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarLiquidationTaxParam(DataGridViewRowValues.GetValue<int>(row, PL_LiquidationTax.Year),
                                                DataGridViewRowValues.GetValue<int>(row, PL_LiquidationTax.Month));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarJETemplatesParam(text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }
    }

    #endregion

    #region Radar RadarLiquidationType
    internal class RadarLiquidationType : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;

        public RadarLiquidationType()
            : base()
        {
            rdrCodeColumn = PL_LiquidationType.Year.Name;
            rdrDescColumn = "";
            rdrNameSpace = "Plumber.Plumber.Accounting.Forms.LiquidationType";
        }

        protected override Table GetRadarTable()
        {
            return new PL_LiquidationType();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            qb.Clear();
            qb.AddSelectAll(PL_LiquidationType.Name);
            qb.AddFrom(PL_LiquidationType.Name);
            qb.AddCompare(PL_LiquidationType.Year, "@p1");

            p1 = new SqlABParameter("@p1", PL_LiquidationTax.Year);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param.Params[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = ""; 
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            qb.Clear();
            qb.AddSelectAll(PL_LiquidationType.Name);
            qb.AddFrom(PL_LiquidationType.Name);
            qb.AddFilter(findQuery);

            qb.AddOrder(PL_LiquidationType.Year);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarLiquidationTypeParam(DataGridViewRowValues.GetValue<int>(row, PL_LiquidationType.Year));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarLiquidationTypeParam(int.Parse(text));
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }
    }

    #endregion
}